package com.itheima.mall.dubbo.api.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.utils.StringUtils;
import com.itheima.mall.dubbo.mapper.sms.HomeBrandMapper;
import com.itheima.mall.entity.domain.sms.HomeBrand;
import com.itheima.mall.entity.domain.vo.smsre.HomeBrandResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;


@DubboService
public class HomeBrandServiceImpl extends ServiceImpl<HomeBrandMapper, HomeBrand> implements HomeBrandService {
    @Autowired
    private HomeBrandMapper homeBrandMapper;

    @Override
    public void updateSort(Long id, Integer sort) {
        HomeBrand homeBrand = new HomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);
        homeBrandMapper.updateById(homeBrand);
    }

    @Override
    public HomeBrandResult page(Integer pageNum, Integer pageSize, Integer recommendStatus, String brandName) {
        Page<HomeBrand> homeBrandPage = new Page<>(pageNum,pageSize);
        HomeBrandResult brandResult = new HomeBrandResult();
        LambdaQueryWrapper<HomeBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(brandName), HomeBrand::getBrandName, brandName);
        queryWrapper.eq(recommendStatus != null, HomeBrand::getRecommendStatus, recommendStatus);
        homeBrandMapper.selectPage(homeBrandPage, queryWrapper);
        brandResult.setList(homeBrandPage.getRecords());
        brandResult.setPageNum(pageNum);
        brandResult.setPageSize(pageSize);
        brandResult.setTotal(homeBrandPage.getTotal());
        brandResult.setTotalPage(homeBrandPage.getPages());

        return brandResult;
    }


}
